#include "zglDrawContext.h"
#include "zglEngine.h"

namespace sgui
{
namespace engine
{
	
bool zglDrawContext::beginDraw(sDrawSurface& surface) const
{
	if (getClipRect())
		if (m_drawRect.W <= 0 || m_drawRect.H <= 0) 
		{
			return false;
		}

	glPushMatrix();

	updateDraw(surface);

	glTranslatef(m_rect.X, m_rect.Y, 0);
	glTranslatef(m_scroll.X, m_scroll.Y, 0);

	return true;
}

void zglDrawContext::endDraw(sDrawSurface& surface) const
{
	glPopMatrix();	
	
	if (getClipRect())
	{
		m_engine.removeScissor();
	}
}
	
void zglDrawContext::updateDraw(sDrawSurface& surface) const
{
	if (getClipRect())
	{
		m_engine.setScissor(m_drawRect, surface);
	}
}


void zglDrawContext::pushTransform(const obj::sTransform& transform) const
{
	glPushMatrix();

	const math::sPointf &position = transform.getPosition();
	const math::sPointf &scale = transform.getScale();
	const f32 &rotation = transform.getRotation();

	glTranslatef(position.X, position.Y, 0);
	glScalef(scale.X, scale.Y, 0);
	glRotatef(rotation, 0, 0, 1);

}

void zglDrawContext::popTransform() const
{
	glPopMatrix();	
}


zglDrawContext::zglDrawContext(const zglEngine& engine, const math::sRectf& rect, const sDrawContext* parentContext, bool clipRect) :
	m_engine(engine),
	sDrawContext(engine, rect, parentContext, clipRect)
{
	//
}

zglDrawContext::zglDrawContext(const zglEngine& engine, const math::sRectf& rect, bool clipRect) :
	m_engine(engine),
	sDrawContext(engine, rect, clipRect)
{
	//
}


};
};